package com.lb.shopguide.ui.fragment.guide.member;

import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.lb.shopguide.R;
import com.lb.shopguide.adapter.viewpager.AdapterMemberType;
import com.lb.shopguide.base.BaseMainFragment;
import com.lb.shopguide.event.jump.JumpMemberListEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by kang on 2017/9/7.
 */

public class FragmentMember extends BaseMainFragment {
    @BindView(R.id.tabs)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    private List<Fragment> mFragmentList;
    private List<String> titles;

    private AdapterMemberType mAdapterMember;

    public static FragmentMember newInstance() {
        return new FragmentMember();
    }



    @Override
    public int getLayoutId() {
        return R.layout.fragment_member;
    }

    @Override
    public void initData() {
        mFragmentList = new ArrayList<>();
        titles = new ArrayList<>();
        EventBus.getDefault().register(this);
    }

    @Override
    public void initView() {


        bindType();
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {

    }

    @Override
    public String getPageTag() {
        return FragmentMember.class.getName();
    }

    private void bindType() {
        initTitles();
        initFragment();

        mAdapterMember = new AdapterMemberType(getChildFragmentManager(), mFragmentList, titles);
        mViewPager.setAdapter(mAdapterMember);
        mViewPager.setOffscreenPageLimit(4);
        mTabLayout.setViewPager(mViewPager);
    }

    private void initTitles() {
        titles.add("消息");
        titles.add("会员");
        titles.add("粉丝");
    }

    private void initFragment() {
        mFragmentList.add(FragmentChatList.newInstance());
        mFragmentList.add(FragmentMemberList.newInstance());
        mFragmentList.add(FragmentFansList.newInstance());
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @Subscribe(sticky = true)
    public void jumpToMessageList(JumpMemberListEvent event) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mTabLayout.setCurrentTab(1, true);
            }
        }, 500);
    }
}
